/* 
 * File:   Log.cpp
 * Author: Gab'z
 * 
 * Created on 4 juin 2012, 09:42
 */

#include "Log.h"
#include "../../../Context.h"
#include "../../literal/Stack.h"
#include "../../literal/Complex.h"
#include "../../literal/Real.h"

namespace calculator {

    Log::Log(const std::string name) : UnaryOperator(name) {
    }

    Log::Log(const Log& orig) : UnaryOperator(orig._name) {
    }

    Log::~Log() {
    }

    std::string Log::isExecutable() const {
        const Complex* complex = dynamic_cast<const Complex*> (Context::getInstance()->getCurrentStack()->getConstant(0));
        if (complex != 0 && Context::getInstance()->getParameters()->getComplexMode() == false) return "Complex mode OFF";
        return "";
    }

    Log* Log::clone() const {
        return new Log(_name);
    }

    const Number* Log::apply(const Number* number) const throw (ArithmeticException) {
        const Complex* complex = dynamic_cast<const Complex*> (number);
        if (complex != 0) throw ArithmeticException("Log of a complex");
        const SimpleNumber* sn =  dynamic_cast<const SimpleNumber*> (number);
        const Real* r = new Real(std::log10(sn->value()));
        return r;
    }

}